package com.ruoyi.sbrk.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sbgl.sbxxgl.domain.Sbxxgl;
import com.ruoyi.sbrk.domain.SbglSbrk;
import com.ruoyi.sbrk.service.ISbglSbrkService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 设备入库Controller
 *
 * @author huacai
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/sbrk/sbrk")
public class SbglSbrkController extends BaseController {
    @Resource
    private ISbglSbrkService sbglSbrkService;

    /**
     * 查询设备入库列表
     */
    @PreAuthorize("@ss.hasPermi('sbrk:sbrk:list')")
    @GetMapping("/list")
    public TableDataInfo list(SbglSbrk sbglSbrk) {
        startPage();
        List<SbglSbrk> list = sbglSbrkService.selectSbglSbrkList(sbglSbrk);
        return getDataTable(list);
    }

    /**
     * 导出设备入库列表
     */
    @PreAuthorize("@ss.hasPermi('sbrk:sbrk:export')")
    @Log(title = "设备入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbglSbrk sbglSbrk) {
        List<SbglSbrk> list = sbglSbrkService.selectSbglSbrkList(sbglSbrk);
        ExcelUtil<SbglSbrk> util = new ExcelUtil<SbglSbrk>(SbglSbrk.class);
        util.exportExcel(response, list, "设备入库数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SbglSbrk> util = new ExcelUtil<SbglSbrk>(SbglSbrk.class);
        util.importTemplateExcel(response, "设备入库数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "设备入库", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sbrk:sbrk:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SbglSbrk> util = new ExcelUtil<SbglSbrk>(SbglSbrk.class);
        InputStream inputStream = file.getInputStream();
        List<SbglSbrk> list = util.importExcel(inputStream);
        // 对导入的数据进行处理
        for (SbglSbrk sbglSbrk : list) {
            sbglSbrk.setSbrkId(IdUtils.fastSimpleUUID());
            sbglSbrk.setIdWhere("存在");
            sbglSbrk.setDeptId(Math.toIntExact(getDeptId()));
            sbglSbrk.setUserId(Math.toIntExact(getUserId()));
        }
        inputStream.close();
        int count = sbglSbrkService.batchInsertSbglSbrk(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取设备入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbrk:sbrk:query')")
    @GetMapping(value = "/{sbrkId}")
    public AjaxResult getInfo(@PathVariable("sbrkId") String sbrkId) {
        return success(sbglSbrkService.selectSbglSbrkBySbrkId(sbrkId));
    }

    /**
     * 新增设备入库
     */
    @PreAuthorize("@ss.hasPermi('sbrk:sbrk:add')")
    @Log(title = "设备入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbglSbrk sbglSbrk) {
        //用户ID
        sbglSbrk.setUserId(Math.toIntExact(getUserId()));
        //部门ID
        sbglSbrk.setDeptId(Math.toIntExact(getDeptId()));
        sbglSbrk.setIdWhere("存在");
        return toAjax(sbglSbrkService.insertSbglSbrk(sbglSbrk));
    }

    /**
     * 修改设备入库
     */
    @PreAuthorize("@ss.hasPermi('sbrk:sbrk:edit')")
    @Log(title = "设备入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbglSbrk sbglSbrk) {
        return toAjax(sbglSbrkService.updateSbglSbrk(sbglSbrk));
    }

    /**
     * 删除设备入库
     */
    @PreAuthorize("@ss.hasPermi('sbrk:sbrk:remove')")
    @Log(title = "设备入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sbrkIds}")
    public AjaxResult remove(@PathVariable String[] sbrkIds) {
        return toAjax(sbglSbrkService.deleteSbglSbrkBySbrkIds(sbrkIds));
    }

    /**
     * 获得ID
     */
    @GetMapping("/getSbrkId")
    public AjaxResult getSbrkId() {
        String sbrkId = IdUtils.fastSimpleUUID();
        return success(sbrkId);
    }
}
